<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="任务名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入任务名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域" prop="region">
        <el-select v-model="queryParams.region" filterable placeholder="请选择">
          <el-option
              v-for="item in regionList"
              :key="item.ucId"
              :label="item.name"
              :value="item.ucId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="el-icon-plus" size="mini" @click="handleBuild">任务配置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableList" border stripe>
      <el-table-column label="任务名称" align="center" prop="name"/>
      <el-table-column label="任务区域" align="center" prop="regionName"/>
      <el-table-column label="任务内容" align="center" prop="content"/>
      <el-table-column label="领取状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="领取人" align="center" prop="executor"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="primary"
              icon="el-icon-info"
              @click="patrolTaskInfoList(scope.row)"
          >巡检记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="20"
        layout="sizes, prev, pager, next"
        :total="1000"
        style="text-align: right"
    >
    </el-pagination>

    <!-- 生成及重置巡检任务配置信息对话框 -->
    <el-dialog title="任务配置" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="任务类型" prop="taskName">
              <el-input v-model="typeName" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="任务要求" prop="remark">
              <el-input type="textarea" :autosize="{ minRows: 2}" v-model="form.remark" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务开始时间" prop="startTime">
              <el-time-picker
                  v-model="form.startTime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :picker-options="{selectableRange: '00:00:00 - 23:59:59'}"
              >
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务结束时间" prop="endTime">
              <el-time-picker
                  v-model="form.endTime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :picker-options="{selectableRange: '00:00:00 - 23:59:59'}"
              >
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="任务内容" prop="content">
              <el-input type="textarea" :autosize="{ minRows: 4}" v-model="form.content" placeholder="请输入任务内容"/>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="24">-->
          <!--            <el-form-item label="提醒时间" prop="callValue">-->
          <!--              <el-input v-model="form.callTime" disabled>-->
          <!--                <el-input v-model="form.callValue" slot="prepend" placeholder="请输入分钟数">-->
          <!--                  <template slot="prepend">上班前</template>-->
          <!--                  <template slot="append">分钟</template>-->
          <!--                </el-input>-->
          <!--              </el-input>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="提醒时间" prop="callValue">
              <el-time-picker
                  v-model="form.callTime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :picker-options="{selectableRange: '00:00:00 - 23:59:59'}"
              ></el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提醒岗位" prop="callPost">
              <el-select v-model="callPostList" multiple filterable placeholder="请选择">
                <el-option
                    v-for="item in postList"
                    :key="item.postCode"
                    :label="item.postName"
                    :value="item.postCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" align="center">
        <el-button type="primary" @click="submitForm">生成巡检任务</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {getRegionList} from "@/api/enterprise";
import {buildPatrolTask, getPatrolTask, patrolTaskDataPage} from "@/api/patrolTask";
import {getPostList} from "@/api/post";
import {listTaskType} from "@/api/taskType";

export default {
  name: "taskType",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 定时任务表格数据
      tableList: [],
      // 是否显示弹出层
      open: false,
      //区域搜索项
      regionList: [],
      // 岗位选择项
      postList: [],
      // 查询参数
      queryParams: {
        pageSize: 20,
        pageNum: 1,
        name: undefined,
        region: undefined
      },
      typeName: "巡检任务",
      callPostList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startTime: [{required: true, message: "任务开始时间不能为空"}],
        endTime: [{required: true, message: "任务结束时间不能为空"}],
      }
    };
  },
  created() {
    this.getList()
    this.getRegionList()
    this.getPostList()
  },
  methods: {
    /** 岗位列表 */
    getPostList() {
      getPostList({}).then(res => {
        this.postList = res.data
      })
    },
    /** 区域列表 */
    getRegionList() {
      getRegionList({}).then(res => {
        this.regionList = res.data
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryParams.pageSize = val
      this.handleQuery()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryParams.pageNum = val
      this.handleQuery()
    },
    /** 状态转换 */
    statusFormat(item) {
      if (item.status === 1) {
        return "已领取"
      } else {
        return "待领取"
      }
    },
    /** 查询巡检区域列表 */
    getList() {
      this.loading = true;
      patrolTaskDataPage(this.queryParams).then(res => {
        this.tableList = res.data.list;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.callPostList=[],
      this.form = {
        id: undefined,
        remark: undefined,
        startTime: undefined,
        endTime: undefined,
        callTime: undefined,
        callValue: undefined,
        callPost: undefined,
        content: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 任务配置按钮 */
    handleBuild() {
      this.reset();
      //获取巡检任务配置信息
      getPatrolTask().then(res => {
        if (res.data) {
          this.form = res.data
          this.callPostList = this.form.callPost.split(",")
        }
      })
      //获取巡检任务的名称及要求
      listTaskType({taskType: 'INSPECTION'}).then(res => {
        if (res.data) {
          this.typeName = res.data[0].taskName
          this.form.remark = res.data[0].taskDemand
        }
      })
      this.open = true
    },
    /** 巡检记录按钮 */
    patrolTaskInfoList(val) {
      this.$router.push("/taskManage/patrolTask/info/" + val.id)
    },
    /** 生成巡检任务按钮 */
    submitForm: function () {
      this.form.callPost = this.callPostList.join(",")
      this.$refs["form"].validate(valid => {
        if (valid) {
          buildPatrolTask(this.form).then(res => {
            if (res.data) {
              this.$message.success("生成成功")
            } else {
              this.$message.error('生成失败')
            }
            this.open = false;
            this.getList();
          });
        }
      })
    }
  }
};
</script>
